package com.hancher.gamelife2.main;

import androidx.room.AutoMigration;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import com.hancher.gamelife2.account.AccountPO;
import com.hancher.gamelife2.account.IAccountDao;
import com.hancher.gamelife2.birthday.BirthdayPO;
import com.hancher.gamelife2.birthday.IBirthdayDao;
import com.hancher.gamelife2.colockin.ColockInPO;
import com.hancher.gamelife2.colockin.ColockInTypePO;
import com.hancher.gamelife2.colockin.IColockInDao;
import com.hancher.gamelife2.colockin.IColockInTypeDao;
import com.hancher.gamelife2.diary.DiaryPO;
import com.hancher.gamelife2.diary.IDiaryDao;
import com.hancher.gamelife2.function.FunctionPO;
import com.hancher.gamelife2.function.IFunctionDao;
import com.hancher.gamelife2.matter.IMatterDao;
import com.hancher.gamelife2.matter.MatterPO;
import com.hancher.gamelife2.menstrual.IMenstrualDao;
import com.hancher.gamelife2.menstrual.MenstrualPO;
import com.hancher.gamelife2.position.IPositionDao;
import com.hancher.gamelife2.position.PositionPO;
import com.hancher.gamelife2.weather.IWeatherDao;
import com.hancher.gamelife2.weather.WeatherPO;

/**
 * 描述 : 数据库工具类 <br/>
 * 时间 : 2023/12/2 13:53 <br/>
 * 作者 : Hancher ytu_shaoweijie@163.com
 * <p>
 * entities 表
 * version 数据库版本
 * exportSchema 是否导出到文件夹
 */
//1. 修改entities
@Database(
        entities = {FunctionPO.class, MatterPO.class, PositionPO.class, WeatherPO.class, DiaryPO.class,
                BirthdayPO.class, AccountPO.class, ColockInTypePO.class, ColockInPO.class, MenstrualPO.class},
        version = 11,
        exportSchema = true,
        autoMigrations = {
                @AutoMigration(from = 1, to = 2),
                @AutoMigration(from = 2, to = 3),
                @AutoMigration(from = 3, to = 4),
                @AutoMigration(from = 4, to = 5),
                @AutoMigration(from = 5, to = 6),
                @AutoMigration(from = 6, to = 7),
                @AutoMigration(from = 7, to = 8),
                @AutoMigration(from = 8, to = 9),
                @AutoMigration(from = 9, to = 10),
                @AutoMigration(from = 10, to = 11),
        }
)
public abstract class MainDatabase extends RoomDatabase {
    public static final String DATABASE_NAME = "GameLife.sqlite";

    private static MainDatabase databaseInstance;

    public static synchronized MainDatabase getInstance() {
        if (databaseInstance == null) {
            databaseInstance = Room
                    .databaseBuilder(MainApplication.getInstance().getApplicationContext(), MainDatabase.class, DATABASE_NAME)
//                    .addMigrations(MIGRATION_1_2, MIGRATION_2_3)
//                    //迁移数据库如果发生错误，将会重新创建数据库，而不是发生崩溃
//                    .fallbackToDestructiveMigration()
                    .build();
        }
        return databaseInstance;
    }

    public static synchronized void restart() {
        if (databaseInstance != null && !databaseInstance.isOpen()) {
            databaseInstance = null;
            databaseInstance = Room
                    .databaseBuilder(MainApplication.getInstance().getApplicationContext(), MainDatabase.class, DATABASE_NAME)
                    .build();
        }
    }

    //2. 添加dao接口
    public abstract IFunctionDao functionDao();
    public abstract IMatterDao matterDao();
    public abstract IPositionDao positionDao();
    public abstract IWeatherDao weatherDao();
    public abstract IDiaryDao diaryDao();
    public abstract IBirthdayDao birthdayDao();
    public abstract IAccountDao accountDao();

    public abstract IColockInTypeDao colockInTypeDao();

    public abstract IColockInDao colockInDao();

    public abstract IMenstrualDao menstrualDao();

}
